package com.ruoyi.gams.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gams.vo.CardInfoParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gams.mapper.GamsUserMapper;
import com.ruoyi.gams.domain.GamsUser;
import com.ruoyi.gams.service.IGamsUserService;

/**
 * 用户Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-03
 */
@Service
public class GamsUserServiceImpl implements IGamsUserService
{
    @Autowired
    private GamsUserMapper gamsUserMapper;

    /**
     * 查询用户
     *
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public GamsUser selectGamsUserById(Integer id)
    {
        return gamsUserMapper.selectGamsUserById(id);
    }
    @Override
    public GamsUser findFirstByCertnumber(String cardid)
    {
        return gamsUserMapper.findFirstByCertnumber(cardid);
    }

    /**
     * 查询用户列表
     *
     * @param gamsUser 用户
     * @return 用户
     */
    @Override
    public List<GamsUser> selectGamsUserList(GamsUser gamsUser)
    {
        return gamsUserMapper.selectGamsUserList(gamsUser);
    }

    /**
     * 新增用户
     *
     * @param gamsUser 用户
     * @return 结果
     */
    @Override
    public int insertGamsUser(GamsUser gamsUser)
    {
        gamsUser.setCreateTime(DateUtils.getNowDate());
        return gamsUserMapper.insertGamsUser(gamsUser);
    }

    /**
     * 修改用户
     *
     * @param gamsUser 用户
     * @return 结果
     */
    @Override
    public int updateGamsUser(GamsUser gamsUser)
    {
        return gamsUserMapper.updateGamsUser(gamsUser);
    }

    /**
     * 批量删除用户
     *
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteGamsUserByIds(Integer[] ids)
    {
        return gamsUserMapper.deleteGamsUserByIds(ids);
    }

    /**
     * 删除用户信息
     *
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteGamsUserById(Integer id)
    {
        return gamsUserMapper.deleteGamsUserById(id);
    }

    @Override
    public GamsUser addUser(CardInfoParam cardInfoParam) {
        String cardInfo = cardInfoParam.getCardInfo();
//        String replace = cardInfo.replace("\n", "");
        JSONObject object = JSONObject.parseObject(cardInfo);
        String bornDay = object.getString("bornDay");
        String certAddress = object.getString("certAddress");
        String certNumber = object.getString("certNumber");
        String certOrg = object.getString("certOrg");
        String certType = object.getString("certType");
        String effDate = object.getString("effDate");
        String errorMsg = object.getString("errorMsg");
        String expDate = object.getString("expDate");
        String gender = object.getString("gender");
        String identityPic = object.getString("identityPic");
        String nation = object.getString("nation");
        String nationCode = object.getString("nationCode");
        String partyName = object.getString("partyName");
        GamsUser first = gamsUserMapper.findFirstByCertnumber(certNumber);

        GamsUser user = new GamsUser();
        user.setName(partyName);
        user.setTel("");
        user.setCardId(certNumber);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setBornday(bornDay);
        user.setCertaddress(certAddress);
        user.setCertnumber(certNumber);
        user.setCertorg(certOrg);
        user.setCerttype(certType);
        user.setEffdate(effDate);
        user.setErrormsg(errorMsg);
        user.setExpdate(expDate);
        user.setGender(gender);
        user.setIdentitypic(identityPic);
        user.setNation(nation);
        user.setNationcode(nationCode);
        user.setPartyname(partyName);
        if (first == null) {
            gamsUserMapper.insertGamsUser(user);
        } else {
            user.setId(first.getId());
            gamsUserMapper.updateGamsUser(user);
        }
        return user;
    }
}
